﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace University.Workshop.Model.LogicalWinner
{
    public class Winner
    {
        //public static Winner instance;
        //public static Winner Instance { get { if (instance == null)instance = new Winner(); return instance; } }
        //public static TableManager Instance { get { if (instance == null)instance = new TableManager(); return instance; } }

        public Hand[] hands = new Hand[5];         //las 5 manos
        int pointer = 0;                           //puntero de las manos 1-5 

        public int[] WinnerInHands =new int[5] ;   //lista de posiciones de ganadores
        private int pointerWinnerInHands = 0;      //puntero de lista de ganadores

        public string[] highestNumberOfWinning = new string[5];   //lista si hay mas de un ganador por carta mayor
        private int pointerHighestNumberOfWinning = 0;

        //public string[] vector = new string[10] ; //de split de ganador
        public int[] EqualWinner = new int[5];

        public Winner()
        {
            for (int i = 0; i < 5; i++)
            {
                highestNumberOfWinning[i] = "1";
                WinnerInHands[i] = -1;
                EqualWinner[i] = -1;
                hands[i] = new Hand();
            }
        }
        
        // manda un string num typo

        public void SetHand(string numbers, string type)
        {
            char[] auxiliary = { ' ' };
            string[] vectorNumbers = numbers.Split(auxiliary);

            for (int i = 0; i < 7; i++)
            {
                hands[pointer].CardsOfHand[i].NumberOfCard = Convert.ToInt32(vectorNumbers[i], CultureInfo.InvariantCulture);
            }
           
            string[] vectorType = type.Split(auxiliary);
            for (int j = 0; j < 7; j++)
            {
                if (vectorType[j] == "t")
                   hands[pointer].CardsOfHand[j].TypeOfCard = "clover";
                if (vectorType[j] == "e")
                    hands[pointer].CardsOfHand[j].TypeOfCard =  "sword";
                if (vectorType[j] == "c")
                    hands[pointer].CardsOfHand[j].TypeOfCard = "heart";
                if (vectorType[j] == "d")
                    hands[pointer].CardsOfHand[j].TypeOfCard = "diamont";
            }
            hands[pointer].HandState = true;
            pointer = pointer + 1;
        }
        //this.Rank + " " + this.Suit
        //public enum EnumRank
        //{
            // , Five, Six, Seven, Eight,
          //  
        //}
        public void SetCard(string card,int pointer)
        {
            char[] auxiliary = { ' ' };
            string[] result = card.Split( auxiliary );
            string number = ConvertNumber(result[0]);
            string type = ConvertType(result[1]);

            
            for (int j = 0; j < 7; j++)
            {
                if (hands[pointer].CardsOfHand[j].NumberOfCard == -1)
                {
                    hands[pointer].CardsOfHand[j].NumberOfCard = Convert.ToInt32(number,CultureInfo.InvariantCulture);
                    if (type == "t")
                        hands[pointer].CardsOfHand[j].TypeOfCard = "clover";
                    if (type == "e")
                        hands[pointer].CardsOfHand[j].TypeOfCard = "sword";
                    if (type == "c")
                        hands[pointer].CardsOfHand[j].TypeOfCard = "heart";
                    if (type == "d")
                        hands[pointer].CardsOfHand[j].TypeOfCard = "diamont";
                }
            }
        }
        public string ConvertNumber(string card)
        { 
            
            switch(card)
            {
                case "None":
                    return "1";
                case "Ace":
                    return "14";
                case "Two":
                    return "2";
                case "Three":
                    return "3";
                case "Four":
                    return "4";
                case "Five":
                    return "5";
                case "Six":
                    return "6";
                case "Seven":
                    return "7";
                case "Eight":
                    return "8";
                case "Nine":
                    return "9";
                case "Ten":
                    return "10";
                case "Jack":
                    return "11";
                case "Queen":
                    return "12";
                case "King":
                    return "13";
            }
            return "";
        }

        public string ConvertType(string card)
        {
            
            switch (card)
            {
                case "Diamond":
                    return "d";
                case "Heart":
                    return "c";
                case "Club":
                    return "t";
                case "Spade":
                    return "e";
            }
            return "";
        }
        
        public void Restore()
        {
            pointer = 0;
            pointerHighestNumberOfWinning = 0;
            pointerWinnerInHands = 0;

            for (int i = 0; i < 5; i++)
            {
                
                highestNumberOfWinning[i] = "1";
                WinnerInHands[i] = -1;
                EqualWinner[i] = -1;
            }
        }
        public void GetWinner()
        {
            //positionWinner = "1";
            char[] delimiter = { ' ' };
            //vector = positionWinner.Split(delimiter); 

            for (int i = 0; i < 5; i++)
            {
                if (hands[i].HandState)
                {
                    string[] vector2 = (hands[i].HandForce()).Split(delimiter);
                    if (Convert.ToInt32(vector2[0],CultureInfo.InvariantCulture) > Convert.ToInt32(highestNumberOfWinning[pointerHighestNumberOfWinning], CultureInfo.InvariantCulture))
                    {
                        onlyOneWinner();
                        WinnerInHands[pointerWinnerInHands] = i;
                        highestNumberOfWinning[pointerHighestNumberOfWinning] = vector2[0];
                    }
                    else
                    {
                        //if (Convert.ToInt32(vector2[0]) == Convert.ToInt32(positionWinner))
                        //{
                          //  equalWinner[i] = i;
                        //}
                    }
                }
            }
            if (possibleTwoWinners())
            {
                for (int i = 0; i < 5; i++)
                {
                    if (EqualWinner[i] != -1)
	                {
                        hands[i].HandForce();
	                }
                      
                }
            }
        }
        private bool possibleTwoWinners()
        {
            for (int i = 0; i < 5; i++)
            {
                if (EqualWinner[i] != -1)
                    return true;
            }
            return false;
        }
        private void onlyOneWinner()
        {
            for (int i = 0; i < 5; i++)
            {
                EqualWinner[i] = -1;
            }
        }





    }
}
